<?php

/*
 * This file is part of the overtrue/wechat.
 *
 * (c) overtrue <i@overtrue.me>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

/**
 * Application.php.
 *
 * Part of Overtrue\WeChat.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @author    overtrue <i@overtrue.me>
 * @copyright 2015
 *
 * @see      https://github.com/overtrue
 * @see      http://overtrue.me
 */





class WeixinBase
{

    private $appId;
    private $appSecret;
    private $token;
    public function __construct($config) {
        $this->appId = $config['app_id'];
        $this->appSecret = $config['secret'];
        $this->token = $config['token'];
    }

    //生成推广二维码

    public function createQrcodeUrl($access_token,$params){

       //URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKENPOST数据格式：json
        //POST数据例子：{"expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 123}}}
        $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token;
        $json = $this->httpRequest ( $url,$params);
        $result = json_decode ( $json, true );
        return $result['url'];


    }




    //获取素材接口  https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESS_TOKEN
   //params实例：{"type":news,"offset":0,"count":2}
    public function getBatchgetMaterial($access_token,$params){
        $url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=".$access_token;
        $json = $this->httpRequest ( $url,$params);
        $result = json_decode ( $json, true );
        return $result;
    }




    //获得客服列表;
    public function getKfUserList($access_token){
        $url = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=".$access_token;
        $json = $this->httpRequest ( $url);
        $kf = json_decode ( $json, true );
        return $kf;
    }
    //通过kf接口给用户发信息
    public function sendMsgToUserWithKf($access_token,$touser,$content,$type='text'){
        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;
        $senddata = ['touser'=>$touser,'msgtype'=>$type,'text'=>['content'=>$content]];
        $data = json_encode($senddata,JSON_UNESCAPED_UNICODE);
        //return $data;
        $json = $this->httpRequest ( $url,$data);
        $result = json_decode ( $json, true );
        return $result;

    }

    //创建菜单
    public function createMenu($access_token,$menudata){

        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
        $json = $this->httpRequest ( $url,$menudata);
        $menu = json_decode ( $json, true );
        return $menu;

    }

    //获取菜单
    public function getMenu($access_token){
        $url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".$access_token;
        $json = $this->httpRequest ( $url );
        $menu = json_decode ( $json, true );
        return $menu;

    }


   //获得用户列表
    public function getUserList($nextOpenId = null,$access_token){

         $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$access_token."&next_openid=".$nextOpenId;
        $json = $this->httpRequest ( $url );
        $userlist = json_decode ( $json, true );
        return $userlist;

    }

   //获得用户详情
    public function getUserInfo($openid,$access_token) {
        $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" . $access_token . "&openid=" . $openid . "&lang=zh_CN";
// 		echo "<br>" . $url;
        $json = $this->httpRequest ( $url ); // file_get_contents ( $url ); // 获取微信用户基本信息
// 		echo "<br>json=" . $json;
// 		$this->set_php_file("testUserInfo.txt",$openid.$json."[test]");
        $userInfo = json_decode ( $json, true );
        return $userInfo;
    }
    /*
     * 网页授权，获取access_token，非网页获取用户信息
     * cao，2016-04-11
     */
    public function getPageAccessToken($code) {

        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appId."&secret=".$this->appSecret.
            "&code=".$code."&grant_type=authorization_code";
        $res = json_decode ( $this->httpRequest ( $url ) );
        //如果是未关注用户，上述方法得不到用户更详细的信息，必须采用其它方式
        return $res;
    }
    //获取网页授权用户信息
    public function getPageUserInfo($snstoken,$openid){
        $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$snstoken."&openid=".$openid."&lang=zh_CN";
        $res = json_decode ( $this->httpRequest ( $url ) );
        //如果是未关注用户，上述方法得不到用户更详细的信息，必须采用其它方式
        return $res;
    }
    /*
     * 获取access_token，非网页获取用户信息
     * cao，2016-04-11
     */
    public function getAccessToken() {
        //http请求方式: GET
        //https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
        // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appId."&secret=".$this->appSecret;
        $res = json_decode ( $this->httpRequest( $url ));
        $access_token = $res->access_token;
        return $access_token;
    }
    
    private function httpGet($url) {
        $curl = curl_init ();
        curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, true );
        curl_setopt ( $curl, CURLOPT_TIMEOUT, 500 );
        // 为保证第三方服务器与微信服务器之间数据传输的安全性，所有微信接口采用https方式调用，必须使用下面2行代码打开ssl安全校验。
        // 如果在部署过程中代码在此处验证失败，请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
        curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, true );
        curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, true );
        curl_setopt ( $curl, CURLOPT_URL, $url );

        $res = curl_exec ( $curl );
        curl_close ( $curl );

        return $res;
    }


    public function httpRequest($url,$data = null){
        $curl = curl_init();
        curl_setopt ( $curl, CURLOPT_URL, $url );
        curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, false );
        if(isset($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }








}
